package com.hlw.springboot.controller;

import java.io.IOException;

import com.hlw.springboot.entity.FileInfo;
import com.hlw.springboot.mapper.FileInfoMapper;
import com.hlw.springboot.service.FileService;
import com.hlw.springboot.util.DataResult;
import com.hlw.springboot.util.LayuiFile;
import com.hlw.springboot.util.LayuiFile.LayuiFileData;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;

//@Api(tags = "文件")
@RestController
@RequestMapping("/api/files")
public class FileController {

    @Autowired
    private FileService fileService;
    @Resource
    private FileInfoMapper fileInfoDao;


    @PostMapping
    @ApiOperation(value = "文件上传")
    public FileInfo uploadFile(MultipartFile file) throws IOException {
        return fileService.save(file);
    }

    /**
     * layui富文本文件自定义上传
     *
     * @param file
     * @param domain
     * @return
     * @throws IOException
     */
    @PostMapping("/layui")
    @ApiOperation(value = "layui富文本文件自定义上传")
    public LayuiFile uploadLayuiFile(MultipartFile file, String domain) throws IOException {
        FileInfo fileInfo = fileService.save(file);

        LayuiFile layuiFile = new LayuiFile();
        layuiFile.setCode(0);
        LayuiFileData data = new LayuiFileData();
        layuiFile.setData(data);
        data.setSrc(domain + "/files" + fileInfo.getUrl());
        data.setTitle(file.getOriginalFilename());

        return layuiFile;
    }


    @DeleteMapping("/{id}")
    @ApiOperation(value = "文件删除")
    @RequiresPermissions("sys:file:del")
    public void delete(@PathVariable String id) {
        fileService.delete(id);
    }


    @RequestMapping("/uploadImg")
    @ApiOperation(value = "文件上传")
    public DataResult uploadImg(MultipartFile file) throws IOException {
        FileInfo fileInfo =fileService.save(file);
        return new DataResult(0,"success",fileInfo);
    }

}
